//
// Created by zhouwd on 2020/3/6.
//

#include "jniutil.h"
#include "render.h"
#include <android/asset_manager_jni.h>

OpenglRender *render;

extern "C"
char * getShaderFromAsset(AAssetManager* mgr, const char *mfile) {
    AAsset* assetFragmentShader = AAssetManager_open(mgr, mfile, AASSET_MODE_UNKNOWN);

    if(assetFragmentShader==NULL)
    {
        ALOGE(" %s","asset==NULL");
        return NULL;
    }
    /*获取文件大小*/
    off_t bufferSize = AAsset_getLength(assetFragmentShader);
    //ALOGE("file size : %d\n",bufferSize);
    char *buffer=(char *)malloc(bufferSize+1);
    buffer[bufferSize]=0;
    int numBytesRead = AAsset_read(assetFragmentShader, buffer, bufferSize);
    ALOGE("numBytesRead: %d",numBytesRead);
    ALOGE("buffer: %s",buffer);

    /*关闭文件*/
    AAsset_close(assetFragmentShader);

    return buffer;
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_app_opengllearnmodule_OpenglLearnActivity_glInit(JNIEnv *env, jclass type, jobject assetManager){
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glInit");

    render = new OpenglRender();

    AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);

    if(mgr==NULL)
    {
        ALOGE(" %s","AAssetManager==NULL");
        return JNI_FALSE;
    }

    const char *mPreviewFragmentShaderFile = "opengl_fragment_shader.glsl";
    const char *mPreviewVertexShaderFile = "opengl_vertex_shader.glsl";

    char *mPreviewFragmentShaderBuffer = getShaderFromAsset(mgr, mPreviewFragmentShaderFile);

    char *mPreviewVertexShaderBuffer = getShaderFromAsset(mgr, mPreviewVertexShaderFile);

    int ret;

    ret = render->init(mPreviewFragmentShaderBuffer , mPreviewVertexShaderBuffer);

    if (!ret){
        ALOGE("程序创建失败");
        return JNI_FALSE;
    }

    return 1;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_opengllearnmodule_OpenglLearnActivity_glResize(JNIEnv *env, jclass type, jint width, jint height){
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glResize -> width -> %d -> height -> %d", width, height);
    render->resize(width, height);
    return 1;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_opengllearnmodule_OpenglLearnActivity_glStep(JNIEnv *env, jclass type){
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glStep");

    render->step();
    return 1;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_opengllearnmodule_OpenglLearnActivity_glAddTexturePic(JNIEnv *env, jclass type, jbyteArray data_, jint size, jint width, jint height){
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glAddTexturePic -> size -> %d", size);
    jbyte *data = env->GetByteArrayElements(data_, NULL);
    jint  length = env->GetArrayLength(data_);
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glAddTexturePic -> length -> %d", length);
    char* data1 = (char *)malloc(size);

    memcpy(data1, data, size);
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glAddTexturePic --> data1 -> %d" ,sizeof(data));
    render->setPicTexture(data1, size, width, height);

    free(data1);

    env->ReleaseByteArrayElements(data_, data, 0);
    return 1;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_opengllearnmodule_OpenglLearnActivity_glChange(JNIEnv *env, jclass type) {
    ALOGE("Java_com_app_opengllearnmodule_OpenglLearnActivity_glChange");
    render->change();
    return 0;
}

